package common;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Description:
 * Date:2025-06-16
 * Time:15:36
 */
public class FileUtil {
    // 一次读取整个文件
    public static String readFile(String filePath) {
        StringBuilder result = new StringBuilder();
        try (FileReader fileReader = new FileReader(filePath)) {
            while (true) {
                int ch = fileReader.read();
                if (ch == -1) {
                    break;
                }
                result.append((char)ch);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result.toString();
    }
    // 一次写入整个文件
    public static void writeFile(String filePath, String content) {
        try (FileWriter fileWriter = new FileWriter(filePath)) {
            fileWriter.write(content);
    }catch (IOException e) {
        e.printStackTrace();
        }
    }


//    public static void main(String[] args) {
//        common.FileUtil.writeFile("D:\\test.txt", "hello world");
//        String  content = common.FileUtil.readFile("D:\\test.txt");
//        System.out.println(content);
//    }
}
